package com.shopcart.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "shop_product")
public class Product implements java.io.Serializable {

	private static final long serialVersionUID = -3091227002340481739L;

	@Id
	@GeneratedValue
	private Long id;

	private String name;

	private Integer quantity;

	@ManyToMany(fetch = FetchType.LAZY)
	private List<Product> matchingProducts = new ArrayList<Product>();

	public Product() {
	}

	public Product(Long id, String name, Integer quantity) {
		this.id = id;
		this.name = name;
		this.quantity = quantity;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getQuantity() {
		return quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public List<Product> getMatchingProducts() {
		return matchingProducts;
	}

	public void setMatchingProducts(List<Product> matchingProducts) {
		this.matchingProducts = matchingProducts;
	}

	public String getMatchingProductsString() {
		String result = "";
		if (matchingProducts.size() > 1) {
			result = matchingProducts.get(0).name;
			for (int i=1; i<matchingProducts.size(); i++)
				result += ", " + matchingProducts.get(i);

		}
		return result;
	}
}